﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavascriptOjbectDynamic.aspx.cs"
    Inherits="JavascriptTest.JavascriptOjbectDynamic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Javascript动态扩展对象</title>
    <script type="text/javascript">
        function Employee() {
            this.id = 1;
            this.name = "DynamicExtension";
        }

        //动态扩展对象最简单的方法
        function newObject() {
            var empObj = new Employee();
            empObj["age"] = 20;
            empObj["toString"] = function () { return this.id.toString() + this.name; };
            alert(empObj.toString());
        }

        //扩展对象简单提取成方法
        function dynamicSetProperty(obj, key, value) {
            //注意：在把非布尔值转换成布尔值时：
            //任何对象都为true
            //字符串为空的时候为false，数字0为false，null和undefine为false
            if (obj && key) {
                obj[key] = value;
            }
        }

        function extendObject() {
            var empObj = new Employee();
            alert(empObj.toString());
            dynamicSetProperty(empObj, "age", 20);
            dynamicSetProperty(empObj, "toString", function () { return this.name.toString(); });
            alert(empObj.toString());
        }

        //扩展对象的方法赋值放到另外的函数中去执行，扩展行为可自己定义
        function dynamicObjetPropertyWithSetMethod(obj, key, value, fn) {
            /// <summary>
            ///  扩展对象的属性
            ///</summary>
            ///<param name="obj" type="Object">被扩展的对象</param>
            ///<param name="key" type="Object">需要扩展的属性名称</param>
            ///<param name="value" type="Object">扩展属性的值</param>
            ///<param name="fn" type="Method">扩展属性执行的方法</param>
            if (obj && key) {
                fn(obj, key, value);
            }
        }

        function extendObjectWithSetMethod() {
            var empObj = new Employee();
            alert(empObj.toString());
            dynamicObjetPropertyWithSetMethod(empObj, "age", 20, dynamicSetProperty);
            dynamicObjetPropertyWithSetMethod(empObj, "toString", (function () { return this.name.toString(); }), dynamicSetProperty);
            alert(empObj.toString());
        }

        //扩展对象属性考虑到值为函数的情况，
        //exec把值分为两种情况，直接把值函数赋值给属性或者把值函数的运行结果赋值给属性
        function extenObjectValueFunction(obj, key, value, fn, exec) {
            if (obj && key) {
                var temp = value;
                if (exec)
                    temp = value.call(obj, key, fn(obj, key));
                fn(obj, key, temp);
            }
        }

        function Student() {
            this.id = 1;
            this.name="JackieHan";
        }

        function extendObjectValueFunction() {
            var empObj = new Employee();
            alert(empObj.age);
            extenObjectValueFunction(empObj, "age", function (key, value) { return value + 10; },dynamicSetProperty, true);
            alert(empObj.age);
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <a href="#" onclick="newObject()">点击执行newObject</a>
        <br />
        <a href="#" onclick="extendObject()">点击执行ExtendObject</a>
        <br />
        <a href="#" onclick="extendObjectWithSetMethod()">点击执行extendObjectWithSetMethod</a>
        <br />
        <a href="#" onclick="extendObjectValueFunction()">点击执行extendObjectValueFunction</a>
    </div>
    </form>
</body>
</html>
